<script setup lang="ts">
import { ref } from 'vue';
import { $t } from '@/locales';
import { type XxlJobInfoSearchParams, fetchXxlJobGroupOptions } from '@/service/api';

defineOptions({
  name: 'JobInfoSearch'
});

interface Emits {
  (e: 'reset'): void;
  (e: 'search'): void;
}

const emit = defineEmits<Emits>();

const model = defineModel<XxlJobInfoSearchParams>('model', { required: true });

function reset() {
  emit('reset');
}

function search() {
  emit('search');
}
const jobGroupOptions = ref<CommonType.Option<number>[]>([]);
async function getJobGroupOptions() {
  const { error, data } = await fetchXxlJobGroupOptions();

  if (!error) {
    const options = data.map(item => ({
      label: item.label,
      value: item.value
    }));

    jobGroupOptions.value = [...options];
  }
}

const triggerStatusOptions = [
  {
    label: $t('page.base.xxl.job.all'),
    value: -1
  },
  {
    label: $t('page.base.xxl.job.stop'),
    value: 0
  },
  {
    label: $t('page.base.xxl.job.running'),
    value: 1
  }
];

getJobGroupOptions();
</script>

<template>
  <NCard :title="$t('common.search')" :bordered="false" size="small" class="card-wrapper">
    <NForm :model="model" label-placement="left" :label-width="100">
      <NGrid responsive="screen" item-responsive>
        <NFormItemGi span="24 s:12 m:5" :label="$t('page.base.xxl.job.jobGroup')" path="jobGroup" class="pr-24px">
          <NSelect
            v-model:value="model.jobGroup"
            clearable
            :placeholder="$t('page.base.xxl.job.form.jobGroup')"
            :options="jobGroupOptions"
          />
        </NFormItemGi>
        <NFormItemGi span="24 s:6 m:2" path="triggerStatus" class="pr-24px">
          <NSelect
            v-model:value="model.triggerStatus"
            default-value="-1"
            clearable
            :placeholder="$t('page.base.xxl.job.form.triggerStatus')"
            :options="triggerStatusOptions"
          />
        </NFormItemGi>
        <NFormItemGi span="24 s:12 m:4" path="jobDesc" class="pr-24px">
          <NInput v-model:value="model.jobDesc" clearable :placeholder="$t('page.base.xxl.job.form.jobDesc')" />
        </NFormItemGi>
        <NFormItemGi span="24 s:12 m:4" path="executorHandler" class="pr-24px">
          <NInput
            v-model:value="model.executorHandler"
            clearable
            :placeholder="$t('page.base.xxl.job.form.executorHandler')"
          />
        </NFormItemGi>
        <NFormItemGi span="24 s:12 m:4" path="author" class="pr-24px">
          <NInput v-model:value="model.author" clearable :placeholder="$t('page.base.xxl.job.form.author')" />
        </NFormItemGi>
        <NFormItemGi span="24 s:12 m:3">
          <NSpace class="w-full" justify="end">
            <NButton @click="reset">
              <template #icon>
                <icon-ic-round-refresh class="text-icon" />
              </template>
              {{ $t('common.reset') }}
            </NButton>
            <NButton type="primary" ghost @click="search">
              <template #icon>
                <icon-ic-round-search class="text-icon" />
              </template>
              {{ $t('common.search') }}
            </NButton>
          </NSpace>
        </NFormItemGi>
      </NGrid>
    </NForm>
  </NCard>
</template>

<style scoped></style>
